"""Definition of the ResearchEvent content type
"""

from zope.interface import implements
from Products.CMFCore.utils import getToolByName
from Products.ATContentTypes.content import event
try:
    from Products.LinguaPlone import public as atp
except ImportError:
    # No multilingual support
    from Products.Archetypes import atapi as atp
#from Products.ATContentTypes.content import base
from Products.ATContentTypes.content import schemata
#
## -*- Message Factory Imported Here -*-
from my315ok.researchevent import researcheventMessageFactory as _
#
from Products.ATContentTypes.content import folder
from my315ok.researchevent.interfaces import IResearchEvent
from my315ok.researchevent.config import PROJECTNAME

ResearchEventSchema = folder.ATFolderSchema.copy()  +  atp.Schema((

    # -*- Your Archetypes field definitions here ... -*-    

    atp.DateTimeField(
        'occurrencetime',
        storage=atp.AnnotationStorage(),
        
        widget=atp.CalendarWidget(
            label=_(u"Time"),            
            starting_year = 1949,
            future_years = 1,
            show_hm = True,
            format ="%Y/%m/%d %H:%M:%S",
            description=_(u"Time"),
        ),
        required=True,
        validators=('isValidDate'),
    ),


    atp.StringField(
        'lgtd',
        storage=atp.AnnotationStorage(),
        widget=atp.StringWidget(
            label=_(u"Logitude"),
            description=_(u"Logitude"),
        ),
        required=True,
        default = "°W",
        
    ),


    atp.StringField(
        'latd',
        storage=atp.AnnotationStorage(),
        widget=atp.StringWidget(
            label=_(u"latitude"),
            description=_(u"latitude"),
        ),
        required=True,
        default = "°N",
        
    ),


    atp.StringField(
        'depth',
        storage=atp.AnnotationStorage(),
        widget=atp.StringWidget(
            label=_(u"Depth"),
            description=_(u"depth of earthquake source"),
        ),
        required=True,
        validators=('isDecimal'),
    ),


    atp.StringField(
        'area',
        searchable = True,
        storage=atp.AnnotationStorage(),
        widget=atp.StringWidget(
            label=_(u"Region"),
            description=_(u"Region"),
        ),
        required=True,
    ),


    atp.StringField(
        'earthquakelevel',
        storage=atp.AnnotationStorage(),
        searchable = True,
        widget=atp.StringWidget(
            label=_(u"Magnitude"),
            description=_(u"Magnitude"),
        ),
        validators=('isDecimal'),
    ),
    atp.StringField(
        'eqlevelunit',
        storage=atp.AnnotationStorage(),
        widget=atp.StringWidget(
            label=_(u"Magnitude unit"),
            description=_(u"Magnitude unit"),
        ),
        default = "Mw"
    ),
    atp.TextField(
        'text',
        default_content_type = 'text/restructured',
        default_output_type = 'text/x-html-safe',
        searchable = True,
        allowable_content_types=('text/plain', 'text/restructured', 'text/html',), 
        storage=atp.AnnotationStorage(),
        widget=atp.RichWidget(
            label=_(u"Body text"),
            description=_(u"Body text"),
            allow_file_upload=False,
        ),
    ),
    atp.TextField(
        'comment',
        default_content_type = 'text/restructured',
        default_output_type = 'text/x-html-safe',
        searchable = True,
        allowable_content_types=('text/plain', 'text/restructured', 'text/html',),
        storage=atp.AnnotationStorage(),
        widget=atp.RichWidget(
            label=_(u"comments"),
            description=_(u"additional comments"),
            allow_file_upload=False,
        ),
    ),
))

# Set storage on fields copied from ATContentTypeSchema, making sure
# they work well with the python bridge properties.

ResearchEventSchema['title'].storage = atp.AnnotationStorage()
ResearchEventSchema['description'].storage = atp.AnnotationStorage()

schemata.finalizeATCTSchema(ResearchEventSchema,folderish=True,moveDiscussion=False)


class ResearchEvent(folder.ATFolder):
    """a event type for research department"""
    implements(IResearchEvent)

    meta_type = "ResearchEvent"
    schema = ResearchEventSchema

    title = atp.ATFieldProperty('title')
    description = atp.ATFieldProperty('description')
    
    def initializeArchetype(self,**kwargs):
        try:
            self.initializeLayers()
            self.markCreationFlag()
            self.setDefaults()
            if kwargs:
                self.edit(**kwargs)
            self._signature = self.Schema().signature()
            if self.isPrincipiaFolderish:
                self.copyLayoutFromParent()
        except ConflictError:
            raise
        except Exception, msg:
            LOG.warn('Exception in initializeArchetype', exc_info=True)
            if DEBUG and str(msg) not in ('SESSION',):
                # debug code
                raise        
        # Enable  syndication by default
        syn_tool = getToolByName(self, 'portal_syndication', None)
        if syn_tool is not None:
            if (syn_tool.isSiteSyndicationAllowed() and
                                    not syn_tool.isSyndicationAllowed(self)):
                syn_tool.enableSyndication(self)
        return 

#     -*- Your ATSchema to Python Property Bridges Here ... -*-
    comment = atp.ATFieldProperty('comment')

    text = atp.ATFieldProperty('text')

    occurrencetime = atp.ATFieldProperty('occurrencetime')

    lgtd = atp.ATFieldProperty('lgtd')

    latd = atp.ATFieldProperty('latd')

    depth = atp.ATFieldProperty('depth')

    area = atp.ATFieldProperty('area')

    earthquakelevel = atp.ATFieldProperty('earthquakelevel')


atp.registerType(ResearchEvent, PROJECTNAME)
